# -*- coding: utf-8 -*-
# #############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-20uid0 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from report import report_sxw
import time
import pooler
from operator import itemgetter


class product(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context=None):
        super(product, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'time': time,
            'get_infor_product': self.get_infor_product,
        })


    def get_infor_product(self):
        cr = self.cr
        uid = self.uid
        product = self.pool.get('product.product')
        company_obj = self.pool.get('res.company')
        warehouse_obj = self.pool.get('stock.warehouse')
        location_obj = self.pool.get('stock.location')
        stock_warehouse_orderpoint_obj = self.pool.get('stock.warehouse.orderpoint')
        send_email_user_obj = self.pool.get('send.email.user')
        product_obj = self.pool.get('product.product')
        mail_group_member_obj = self.pool.get('mail.group.member')
        
        lst_warehouse_ids = warehouse_obj.search(cr, uid, [])
        list_pr = []
        for warehouse in warehouse_obj.browse(cr, uid, lst_warehouse_ids):
            
            lo = warehouse_obj.browse(cr, uid, warehouse.id).lot_stock_id
            location_ids = []
            if lo:
                location_ids = location_obj.search(cr, uid, [('location_id', 'child_of', [lo.id])])
            else:
                location_ids = [lo.id]
            for location in location_obj.browse(cr, uid, location_ids): 
                ctx = {'location': location.id,}
                product_point_ids = stock_warehouse_orderpoint_obj.search(cr, uid, [
                                    ('warehouse_id', '=', warehouse.id)])   
                stt = 0
                for product_point in stock_warehouse_orderpoint_obj.browse(cr, uid, product_point_ids):
                    product = product_obj.browse(cr, uid, product_point.product_id.id, ctx)
                    if product.qty_available <= product_point.product_min_qty:
                        stt+=1
                        list_pr.append({
                            'stt': stt,
                            'product': product and product.name or '',
                            'qty': product.qty_available or 0.0,
                            'qty_min': product_point.product_min_qty or 0.0,
                            'company': location.name or '',
                        })                        
                        

        return list_pr


report_sxw.report_sxw('report.product.report', 'product.product', 'addons/product_report/report/product.rml',
                      parser=product, header="external")

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

